<?php
include '../../../wp-load.php';
get_currentuserinfo();
global $user_level;
if($user_level == 10) {

}
else{
	$url = get_option('siteurl') . '/wp-login.php?action=logout';
	header("Location: $url");exit();
}





################################################################################################################
if($_GET[action] == 'upload'){
	
	$filename      	= 'csvfile';
	$table_name1   	= is_dbtable_there('lkeys');
	
	
	$delimit       	= "\n";
	$prod_quantity	= 1000;
	$row 			= 1;
	$lkeys 			= 1;



	if((FALSE == empty($_FILES[$filename]['tmp_name'])) || ($_POST[name_file] != '')) {


		$handle = fopen($_FILES[$filename]['tmp_name'], "r");

		while (($data = fgetcsv($handle, 1000, $delimit)) !== FALSE) {

		    $row++;
		    $len 		= strlen($data[0]);
			$data[0]	= trim($data[0]);	
			
		    if($len > 0){
			###### Insert into lkeys table #####################
			$column_array 	= array();
			$value_array   	= array();

			$column_array[0] 	= 'lid';      			$value_array[0]   	= '';
			$column_array[1] 	= 'filename';      		$value_array[1]   	= $_POST[name_file];
			$column_array[2] 	= 'lkey';      			$value_array[2]   	= $data[0];
			$column_array[3] 	= 'used';    			$value_array[3]   	= '0';	

			
			$result_db1 = db_insert($table_name1, $column_array, $value_array);


			                         if($result_db1 != 1){
			                             echo "<p>There was a problem with $table_name1 !</p>\n";
			                         }
			$lkeys++;
			}  

		}      
	fclose($handle);

	$lkeys = $lkeys -1;

	header("Location: lkeys.php?showResult=1&a={$lkeys}");
	}  
	else {
		echo "There was an error with the file upload!";
		if(!$_POST[name_file]){
			echo "<br/>Please enter the exact name of the file the license keys are used for.";
		}
		else {
			echo "<br/>You forget to enter a .txt file.";
		}
	}
}

?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
		<html xmlns='http://www.w3.org/1999/xhtml' <?php language_attributes(); ?>>

	<head profile='http://gmpg.org/xfn/11'>	
		<link media="all" type="text/css" href="<?php bloginfo('template_url'); ?>/css/shop_admin.css" rel="stylesheet"></link>
	<title>eGoods :: Shop-Backend :: Upload License Keys</title>
	</head>
	<body class="wp-admin">
	<div id="wpwrap">
	<div id="wpcontent">
	<div id="wphead">
	
	<h1>eGoods :: Upload License Keys			
		<span id="viewsite"><a href='<?php bloginfo('url') ?>/wp-admin/themes.php?page=functions.php'>back to Wordpress-Admin</a></span>
	</h1>
	</div>
	<div id="wpbody">
		<div class="wrap">

	<?php 
	
	if($_GET[showResult] == '1'){
		echo "
		<h1>Upload sucessful!</h1>You uploaded $_GET[a] new license keys.
		<hr noshade size='1'/>
		<br/><br/>
		<a href='lkeys.php'>Upload another .txt file</a>
		";

	}
	else {
	
		echo '
		<fieldset>
		<legend>LICENSE KEY UPLOADER</legend>

		<form action="lkeys.php?action=upload" enctype="multipart/form-data" method="post">
			<table>
			<tr>
			 <td>Name of Download File</td>
			 <td><input name="name_file" type="text" maxlength="255"/></td>
			</tr>
			<tr>
			<tr>
			 <td>File with License Keys</td>
			 <td><input name="csvfile" type="file" /></td>
			</tr>
			<tr>
			 <td colspan="2">&nbsp</td>
			</tr>
			<tr>
			 <td>&nbsp;</td>
			 <td><input type="submit" value="Submit" /></td>
			</tr>
			<tr>
			 <td colspan="2">&nbsp</td>
			</tr>
			</table>
		</form>
		</fieldset>

		<br/><br/><br/>

		<font size="-1">[Keep in mind: It has to be a <b>.txt</b> file]</font>
		';
	}
	
	
	echo "</div></div></div></div></body></html>";
?>